
from rest_framework import serializers

from api import models


class ClassSimpleSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.College
        fields = ('id', 'name')


class CollegeSerializer(serializers.ModelSerializer):

    classes = serializers.SerializerMethodField()

    @staticmethod
    def get_classes(college):
        classes = models.class_m.objects.filter(college=college)
        return ClassSimpleSerializer(classes, many=True).data

    class Meta:
        model = models.College
        fields = '__all__'
        

class ClassSerializer(serializers.ModelSerializer):

    stucount = serializers.SerializerMethodField()

    @staticmethod
    def get_stucount(class_m):
        return models.Student.objects.filter(class_m=class_m).count()

    class Meta:
        model = models.College
        fields = '__all__'





class StudentSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.Student
        fields = '__all__'


class StudentSimpleSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.Student
        fields = ('id', 'name')